package java6681.lesson07;

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

import java.awt.event.ActionEvent;
import java.io.File;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author Junichiro
 * @description
 * @date 2021-04-18 15:29
 **/

public class ListFile6681Controller {

    @FXML
    private ListView<File> lvFiles;

    @FXML
    private TextField tfDir;

    @FXML
    private Label lblCount;


    @FXML
    void listAll() {
        /*计数多少文件夹*/
        int countDir = 0;
        /*计数多少文件*/
        int countFile = 0;
        /*输入为空时*/
        if (tfDir.getText().trim().isEmpty()) {
            tfDir.setText("F:\\JavaFX\\Java6681\\java6681\\07Lesson6681\\src\\java6681\\lesson07");
        }
        /*获取目录地址*/
        File file = new File(tfDir.getText());

        /*毫秒时间转换*/
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        ObservableList<File> items = lvFiles.getItems();

        File[] files = file.listFiles();
        /*遍历*/
        for (File file1 : files) {
            if (file1.isDirectory()) {
                String format = formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(file1.lastModified()), ZoneId.of("Asia/Shanghai")));
                items.add(new File("文件夹" + "，" + file1.getName() + ",     " + format + "\n"));
                countDir++;
            }
        }
        for (File file1 : files) {
            if (file1.isFile()) {
                String format = formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(file1.lastModified()), ZoneId.of("Asia/Shanghai")));
                items.add(new File(file1.getName() + ",     " + format + ", " + (file1.length() / 1024 + 1) + "KB"));
                countFile++;
            }
        }
        lblCount.setText("子目录：" + countDir + "，文件：" + countFile);
    }
/*C:\*/
}
